//
//  IManager+NearbyMerchants.m
//  RocketProject
//
//  Created by chen jiedan on 13-5-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "IManager+NearbyMerchants.h"
#import "RCMerchantsListDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NetUrlDefines.h"
#import "RequestUtlity.h"
#import "GloableEngineService.h"
#import "RCActivityDiscountListDataModel.h"

@implementation IManager (NearbyMerchants)




-(int64_t)requestNearbyMerchantsType:(NSString *)merchantsType
                          inDistance:(NSInteger)distance
                     withSoretedType:(kMerchantsSortedType)sortedType
                             forPage:(NSString *)page
                              withRn:(NSString *)rn
                              inCity:(NSString *)cityID
               withMerchantsListType:(KMerchantListType)merchantListType
                      withDistrictID:(NSString *)districtID
                        withLocation:(CLLocation *)location
                        withDelegate:(id<IManagerDelegate>)delegate
{
    if (nil == page) {
        return kInvalidRequestId;
    }
    
    CLLocation *coor = nil;
    if (!location) {
        coor = [[GloableEngineService sharedInstance] locationInfo];
    }else{
        coor = location;
    }
    
    // 异步获取数据
    
    NSString *urlSuffix = nil;
    
    NSString *sortType = nil;
    if (sortedType == kSortedType_Default) {
        sortType = @"default";
    }else if (sortedType == kSortedType_Distance){
        sortType = @"distance";
    }else if (sortedType == kSortedType_Price){
        sortType = @"price";
    }else if (sortedType == kSortedType_PriceAsc){
        sortType = @"priceasc";
    }else if (sortedType == kSortedType_PriceDes){
        sortType = @"pricedesc";
    }else if (sortedType == kSortedType_TotalAsess){
        sortType = @"pinjia";
    }else if (sortedType == kSortedType_Env){
        sortType = @"weisheng";
    }else if (sortedType == kSortedType_Service){
        sortType = @"fuwu";
    }
    
    
//    kSortedType_PriceAsc= 3,
//    kSortedType_TotalAsess= 4,//总体评价
//    kSortedType_Env= 5,//按环境评价
//    kSortedType_Service= 6//按服务评价
    
    NSString *merchantID = nil;
    if ([merchantsType isEqualToString:@"美食"]) {
        merchantID = @"food";
    }else if ([merchantsType isEqualToString:@"休闲娱乐"]) {
        merchantID = @"entertainment";
    }else if ([merchantsType isEqualToString:@"旅游酒店"]) {
        merchantID = @"hotel";
    }else if ([merchantsType isEqualToString:@"购物"]) {
        merchantID = @"shopping";
    }else if ([merchantsType isEqualToString:@"商务助理"]) {
        merchantID = @"assistant";
    }else if ([merchantsType isEqualToString:@"周边景点"]) {
        merchantID = @"travel";
    }else{
        merchantID = [NSString stringWithFormat:@"%@",merchantsType];
    }

    
    if (merchantListType == KMerchantListType_Distance) {
        urlSuffix = [NSString stringWithFormat:kRCMerchantsListPrefix,cityID,[page intValue],10,coor.coordinate.latitude,coor.coordinate.longitude,merchantID,distance,sortType];
    }else{
        urlSuffix = [NSString stringWithFormat:kRCMerchantsDistrictPrefix,cityID,[page intValue],10,coor.coordinate.latitude,coor.coordinate.longitude,merchantID,districtID,sortType];
    }
    
    
    NSString *url = [RequestUtlity getFormattedUrl:urlSuffix];
//    url = @"http://223.4.99.93/17ruyi/api.php?op=app_nearby&action=nearby&cityid=3362&pn=0&rn=5&lat=39.97252&lon=116.332161&type=food&distance=10000&order=default";
    KFLog(@"request url %@",url);
    return [[IDataProvider sharedInstance] asyncFetchDataWithUserInfo:url
                                                             userInfo:nil
                                                      withCachePolicy:IDataCachePolicyLocalFileAndNetwork
                                                   withDataParseClass:[RCMerchantsListDataModel class]
                                                     withDataDelegate:delegate];
    
}

-(int64_t)requestNearbyDiscountsType:(NSString *)discountsType
                          inDistance:(NSInteger)distance
                     withSoretedType:(kMerchantsSortedType)sortedType
                             forPage:(NSString *)page
                              withRn:(NSString *)rn
                              inCity:(NSString *)cityID
                        withDelegate:(id<IManagerDelegate>)delegate
{
    if (nil == page) {
        return kInvalidRequestId;
    }
    
    CLLocation *coor = [[GloableEngineService sharedInstance] locationInfo];
    
    
    // 异步获取数据
    
    NSString *urlSuffix = nil;
    
    NSString *sortType = nil;
    if (sortedType == kSortedType_Default) {
        sortType = @"default";
    }else if (sortedType == kSortedType_Distance){
        sortType = @"distance";
    }else if (sortedType == kSortedType_Price){
        sortType = @"price";
    }
    
  
    if ([discountsType isEqualToString:@"food"]) {
        discountsType = @"1";
    }else if ([discountsType isEqualToString:@"hotel"]){
        discountsType = @"2";
    }else if ([discountsType isEqualToString:@"traffic"]){
        discountsType = @"3";
    }else if ([discountsType isEqualToString:@"travel"]){
        discountsType = @"4";
    }else if ([discountsType isEqualToString:@"entertainment"]){
        discountsType = @"5";
    }else if ([discountsType isEqualToString:@"shopping"]){
        discountsType = @"6";
    }else if ([discountsType isEqualToString:@"assistant"]){
        discountsType = @"7";
    }
    
    urlSuffix = [NSString stringWithFormat:kRCDiscountsListPrefix,cityID,[page intValue],10,coor.coordinate.latitude,coor.coordinate.longitude,discountsType,distance,sortType];
   
    
    NSString *url = [RequestUtlity getFormattedUrl:urlSuffix];
    KFLog(@"request url %@",url);
    return [[IDataProvider sharedInstance] asyncFetchDataWithUserInfo:url
                                                             userInfo:nil
                                                      withCachePolicy:IDataCachePolicyLocalFileAndNetwork
                                                   withDataParseClass:[RCActivityDiscountListDataModel class]
                                                     withDataDelegate:delegate];
}
@end
